﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using System.Text;
using System.Data.Linq;
using System.Web.UI.WebControls;

using SmartProject.Data;
using System.Data;

namespace SmartProject.BusinessObjects
{
    public class Sprint : Entity
    {
        public Sprint() 
        {
            DataParameter[] dataParameters = new DataParameter[6];
            dataParameters[0] = new DataParameter("SprintId", DataType.Integer);
            dataParameters[1] = new DataParameter("Description", 200);
            dataParameters[2] = new DataParameter("Started", DataType.DateTime);
            dataParameters[3] = new DataParameter("EstimatedDays", DataType.Integer);
            dataParameters[4] = new DataParameter("Ended", DataType.DateTime);
            dataParameters[5] = new DataParameter("OwnerId", DataType.Integer);
            base.Initialize(dataParameters);

            base.BusinessRules.Add(new BusinessRules.SprintMaxMinLength());
        }

        public void Populate(DropDownList dropDownList)
        {
            dropDownList.Items.Clear();
            DataTable sprints = base.Find("smartProject_GetSprints");
            if (sprints != null)
            {
                foreach (DataRow row in sprints.Rows)
                {
                    DateTime started = (DateTime)row["Started"];
                    int estimatedDays = Convert.ToInt32(row["EstimatedDays"]);
                    StringBuilder text = new StringBuilder(row["Description"].ToString());
                    int sprintId = Convert.ToInt32(row["SprintId"]);
                    text.Append(" ");
                    text.Append(started.ToShortDateString());
                    text.Append(" - ");
                    text.Append(started.AddDays(estimatedDays).ToShortDateString());
                    dropDownList.Items.Add(new ListItem(text.ToString(), sprintId.ToString()));
                }
            }
        }

        public override void Load()
        {
            base.Load("smartproject_GetSprint");
        }

        public override void Save()
        {
            base.Run("smartProject_AddSprint");
        }

        public override void Update()
        {
            base.Run("smartProject_UpdateSprint");
        }

        public override void Remove()
        {
            base.Run("smartProject_RemoveSprint");
        }
    }
}
